package com.EAsystem.service.Impl;

import com.EAsystem.aop.AutoFillTime;
import com.EAsystem.entity.Grade;
import com.EAsystem.mapper.GradeMapper;
import com.EAsystem.service.GradeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 成绩服务实现类
 */
@Service
public class GradeServiceImpl implements GradeService {

    @Autowired
    private GradeMapper gradeMapper;



    @Override
    public PageInfo<Grade> findAllByPage(int pageNum, int pageSize) {
        // 设置分页参数
        PageHelper.startPage(pageNum, pageSize);
        // 执行查询
        List<Grade> grades = gradeMapper.findAllForPage();
        // 返回分页结果
        return new PageInfo<>(grades);
    }

    @Override
    public List<Grade> findByCondition(Grade grade) {
        return gradeMapper.findByCondition(grade);
    }

    @Override
    public PageInfo<Grade> findByConditionPage(Grade grade, int pageNum, int pageSize) {
        // 设置分页参数
        PageHelper.startPage(pageNum, pageSize);
        // 执行查询
        List<Grade> grades = gradeMapper.findByCondition(grade);
        // 返回分页结果
        return new PageInfo<>(grades);
    }

    @Override
    public PageInfo<Grade> getGradesByTeacherWithPage(Integer teacherId, Long courseId, String studentSearch, int pageNum, int pageSize) {
        // 设置分页参数
        PageHelper.startPage(pageNum, pageSize);
        // 执行查询，直接在数据库层面按教师ID过滤
        List<Grade> grades = gradeMapper.findGradesByTeacher(teacherId, courseId, studentSearch);
        // 返回分页结果
        return new PageInfo<>(grades);
    }

    @Override
    public Grade findById(Long id) {
        return gradeMapper.findById(id);
    }

    @Override
    @Transactional
    @AutoFillTime
    public boolean add(Grade grade) {
        // 设置创建时间和更新时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentTime = sdf.format(new Date());
        grade.setCreateTime(currentTime);
        grade.setUpdateTime(currentTime);
        
        // 计算总分
        calculateTotalScore(grade);
        return gradeMapper.add(grade) > 0;
    }

    @Override
    @Transactional
    @AutoFillTime(createTime = false)
    public boolean update(Grade grade) {
        // 计算总分
        calculateTotalScore(grade);
        return gradeMapper.update(grade) > 0;
    }

    @Override
    @Transactional
    public boolean updateRegularScore(Integer id, Double regularScore) {
        return gradeMapper.updateRegularScore(id, regularScore) > 0;
    }

    @Override
    @Transactional
    public boolean updateExamScore(Integer id, Double examScore) {
        return gradeMapper.updateExamScore(id, examScore) > 0;
    }

    @Override
    @Transactional
    public boolean delete(Integer id) {
        return gradeMapper.delete(id) > 0;
    }


    
    /**
     * 计算总分
     * 总分 = 平时分 * 0.4 + 考试分 * 0.6
     * @param grade 成绩记录
     */
    private void calculateTotalScore(Grade grade) {
        if (grade.getRegularScore() != null && grade.getExamScore() != null) {
            double totalScore = grade.getRegularScore() * 0.3 + grade.getExamScore() * 0.7;
            totalScore = Math.round(totalScore * 100) / 100.0;
            grade.setTotalScore(totalScore);
        }
    }


}